<?php
	class ArticleList_model extends Ci_Model{
		public function __construct(){
			parent::__construct();
		}
 
 		public function get_article($offset, $page_size, $status, $uName){
 			if ( $status == 0 ) {
 				$query = $this->db->query(" select * from mail where status != 3 and to_who = '$uName' order by time desc limit $offset,$page_size");
 			} else {
 				$query = $this->db->query(" select * from mail where status = $status and to_who = '$uName' order by time desc limit $offset,$page_size");
 			}
			return $query->result();
		}
		
		public function move($id, $status){
			$this->db->query(" update mail set status = '$status' where id = '$id' ");
		}

		public function del_forever($id){
			$this->db->query(" delete from mail where id = '$id' ");
		}

		public function get_mail_content($id){
			$query = $this->db->query(" select * from mail where id = '$id' ");
			return $query->result();
		}

		public function get_send_mail_content($id){
			$query = $this->db->query(" select * from send_mail where sid = '$id' ");
			return $query->result();
		}

		public function art_hot($id, $top) {
			if ($top == 0) {
				$this->db->query(" update mail set top = 1 where id = '$id' ");
			} elseif ($top == 1) {
				$this->db->query(" update mail set top = 0 where id = '$id' ");
			}

		}

		public function count_hot($uName, $status = 0) {
			if ($status == 0) {
				$query = $this->db->query(" select * from mail where top = 1 and to_who = '$uName' ");
			} else {
				$query = $this->db->query(" select * from mail where top = 1 and status = $status and to_who = '$uName' ");
			}
			return intval( $query->num_rows() );
		}
		
		public function count_all_hot($uName) {
			$query = $this->db->query(" select * from mail where top = 1 and to_who = '$uName' ");
			$num = intval( $query->num_rows() );

			$query = $this->db->query(" select * from send_mail where top = 1 and send_man = '$uName' ");
			$num += intval( $query->num_rows() );

			return $num;
		}

/*
*	Edit By: 阿诺
*	Time : 2015.7.12
*/
		public function get_send_mail($offset, $page_size, $uName){
 			$query = $this->db->query(" select * from send_mail where send_man = '$uName' order by time desc limit $offset,$page_size");
			return $query->result();
		}

		public function send_indb($title, $to_who, $content, $uName){
			$this->db->query(" insert into send_mail (title, to_who, content, send_man) values ('$title', '$to_who', '$content', '$uName') ");	
			$this->db->query(" insert into mail (title, content, from_who, to_who) values ('$title', '$content', '$uName', '$to_who') ");	

		}

		public function del_send_mail($id){
			$this->db->query(" delete from send_mail where sid = '$id' ");
		}
/*
*	Edit By: 阿诺
*	Time : 2015.7.12
*/
		public function get_search($key, $offset, $page_size){
			$sql = " select * from mail, send_mail where title like '%$key%' order by time desc limit $offset,$page_size ";
			$query = $this->db->query($sql);
			
			return $query->result();
		}
		
		public function get_search_total($key){
			$sql = " select * from mail where title like '%$key%' order by time desc ";
			$query = $this->db->query($sql);
			$num = $query->num_rows(); 

			$sql = " select * from send_mail where title like '%$key%' order by time desc ";
			$query = $this->db->query($sql);
			$num += $query->num_rows(); 

			return $num;
		}

/*
*	Edit By: 阿诺
*	Time : 2015.7.12
*/
		public function get_allUser(){
			$sql = " select username from user ";
			$query = $this->db->query($sql);
			
			return $query->result();
		}
		
	}

?>